
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Reviews;

public class GenericReviewsDAO extends AutoKeys implements org.jetnuke.dao.ReviewsDAO {
	/* (non-Javadoc)
	 * @see org.jetnuke.model.SectionsModel#retrieveByPK(int)
	 */
	public Reviews retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ReviewModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		Reviews  s = new Reviews();
		
		if (rs.next()) {
			fillReviewsFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillReviewsFromResultSet(Reviews s, ResultSet rs)throws Exception {
		s.setCOVER(rs.getString("COVER"));
		s.setDATE(rs.getTimestamp("DATE1"));
		s.setEMAIL(rs.getString("EMAIL"));
		s.setHITS(rs.getInt("HITS"));
		s.setID(rs.getInt("ID"));
		s.setREVIEWER(rs.getString("REVIEWER"));
		s.setRLANGUAGE(rs.getString("RLANGUAGE"));
		s.setSCORE(rs.getInt("SCORE"));
		s.setTEXT(rs.getString("TEXT"));
		s.setTITLE(rs.getString("TITLE"));
		s.setURL(rs.getString("URL"));
		s.setURL_TITLE(rs.getString("URL_TITLE"));
		
	}

	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			Reviews u = new Reviews();
			fillReviewsFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}
	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ReviewModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ReviewsModel#selectOrderIDDesc(int)
	 */
	public List selectOrderIDDesc(int limit) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ReviewModel.selectOrderIDDesc"));
		p.setInt(1,limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ReviewsModel#addNew(org.jetnuke.entities.Reviews)
	 */
	public int addNew(Reviews rv) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("ReviewModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("ReviewsModel.lastGeneratedReviewID"));
		initNewReview(rv, p);
		
		int id = this.executeAutoKeysQuery(p);
				
		return id;
	}

	/**
	 * @param rv
	 * @param p
	 */
	protected void initNewReview(Reviews rv, PreparedStatement p) throws Exception{
		p.setString(1,rv.getCOVER());
		p.setTimestamp(2,new Timestamp(rv.getDATE().getTime()));
		p.setString(3,rv.getEMAIL());
		p.setInt(4,rv.getHITS());
		p.setString(5,rv.getREVIEWER());
		p.setString(6,rv.getRLANGUAGE());
		p.setInt(7,rv.getSCORE());
		p.setString(8,rv.getTEXT());
		p.setString(9,rv.getTITLE());
		p.setString(10,rv.getURL());
		p.setString(11,rv.getURL_TITLE());
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ReviewsModel#searchOrderDateDesc(java.lang.String)
	 */
	public List searchOrderDateDesc(String query) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ReviewModel.searchOrderDateDesc"));
		p.setString(1,query);
		p.setString(2,query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ReviewsModel#selectOrderHitsDesc(int)
	 */
	public List selectOrderHitsDesc(int limit) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ReviewModel.selectOrderHitsDesc"));
		p.setInt(1,limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ReviewsModel#selectOrderDateDesc(int)
	 */
	public List selectOrderDateDesc(int limit) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ReviewModel.selectOrderDateDesc"));
		p.setInt(1,limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public void update(Reviews rv) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ReviewModel.update"));
		p.setString(1,rv.getCOVER());
		p.setTimestamp(2,new Timestamp(rv.getDATE().getTime()));
		p.setString(3,rv.getEMAIL());
		p.setInt(4,rv.getHITS());
		p.setString(5,rv.getREVIEWER());
		p.setString(6,rv.getRLANGUAGE());
		p.setInt(7,rv.getSCORE());
		p.setString(8,rv.getTEXT());
		p.setString(9,rv.getTITLE());
		p.setString(10,rv.getURL());
		p.setString(11,rv.getURL_TITLE());
		p.setInt(12,rv.getID());
		p.executeUpdate();
		p.close();
		
	}

	public void deleteByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ReviewModel.deleteByPK"));
		p.setInt(1,id);
		p.executeUpdate();
		p.close();
	}

	public void incHits(Reviews rv) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ReviewModel.incHits"));
		p.setInt(1,rv.getID());
		p.executeUpdate();
		p.close();
		
	}

}
